package com.jfs.factory;

import org.jetbrains.annotations.NotNull;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description 心跳线程工厂
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022-08-23
 */
public class BeatThreadFactory implements ThreadFactory {
    private final static String SPL = "-";
    private final AtomicInteger id = new AtomicInteger(0);

    private String name;

    public BeatThreadFactory(String name) {
        this.name = name;
    }

    @Override
    public Thread newThread(@NotNull Runnable runnable) {
        String threadName = name + SPL + id.getAndIncrement();
        Thread thread = new Thread(runnable, threadName);
        thread.setDaemon(true);
        return thread;
    }
}
